from ..ss_base import ss_base
"""
1.增加mini ss 策略状态  active  stop;ss判断stop则删除
2.增加到价启用的功能，激活价格设定
3.增加指令行为功能；满足策略算法干什么；业务端只定义对应的指令业务既可
4.增加触发次数定义；使用在策略端使用
5.增加deal_on 业务端定义放在tick还是哪个回调下面
"""
class miniss_base(ss_base):
    def __init__(self,config,pos,bscmd,activeprice,triggertimes):
        super().__init__(config,pos)
        self.bscmd=bscmd
        self.isstop=False
        self.activeprice=activeprice
        self.triggertimes=triggertimes
        self.dealcmd={
            "buyopen":self.deal_buyopen,
            "sellopen": self.deal_sellopen,
            "buyclose": self.deal_buyclose,
            "sellclose": self.deal_sellclose,
            "buy": self.deal_buy,
            "sell": self.deal_sell,
        }
        self.cmdorder = {
            "buyopen": self.pos.buyopen,
            "sellopen": self.pos.sellopen,
            "buyclose": self.pos.buyclose,
            "sellclose": self.pos.sellclose,
            "buy": self.pos.buy,
            "sell": self.pos.sell,
        }
        self.cmd_active_deal = {
            "buyopen": self.active_buyopen,
            "sellopen": self.active_sellopen,
            "buyclose": self.active_buyclose,
            "sellclose": self.active_sellclose,
            "buy": self.active_buy,
            "sell": self.active_sell,
        }
    def deal_on(self,d):
        if self.activeprice:
            if self.bscmd in self.cmd_active_deal.keys():
                r=self.cmd_active_deal[self.bscmd](d)
                if r:
                    if self.bscmd in self.dealcmd.keys():
                        self.dealcmd[self.bscmd](d)
        else:
            if self.bscmd in self.dealcmd.keys():
                self.dealcmd[self.bscmd](d)
    def deal_buyopen(self,d):
        self.deal_buy(d)
    def deal_sellopen(self,d):
        self.deal_sell(d)
    def deal_buyclose(self,d):
        self.deal_buy(d)
    def deal_sellclose(self,d):
        self.deal_sell(d)
    def deal_buy(self,d):
        pass
    def deal_sell(self,d):
        pass
    def active_buyopen(self,d):
        return self.active_buy(d)
    def active_sellopen(self,d):
        return self.active_sell(d)
    def active_buyclose(self,d):
        return self.active_buy(d)
    def active_sellclose(self,d):
        return self.active_sell(d)
    def active_buy(self,d):
        if self.activeprice:
            tick = d["tick"][-1]
            return self.activeprice>=tick["LastPrice"]
        else:
            return True
    def active_sell(self,d):
        if self.activeprice:
            tick = d["tick"][-1]
            return self.activeprice <= tick["LastPrice"]
        else:
            return True
    def stop(self):
        self.isstop=True
    def ontcik(self,d):
        pass
    def on1mbar(self,d):
        pass
    def onbeforeday(self,d):
        pass
    def oncloseday(self,d):
        pass
    def onendday(self,d):
        pass
